#ifndef ZRTSP_CLIENT_MANAGER_H_
#define ZRTSP_CLIENT_MANAGER_H_
#include <memory>
#include <map>
#include <atomic>
#include "cpputility.h"
#include "muduo_base/Mutex.h"

#include "zRtspClient.h"
#include "BasicUsageEnvironment.hh"

class BasicTaskScheduler;
class BasicUsageEnvironment;
class ZRtspClient;
class ZRtspClientManager {
public:
	SINGLETON_DECLARE(ZRtspClientManager);
	ZRtspClientManager()
	:threadRun_(false),
	 eventLoopWatchVariable_(0),
	 pLiveScheduler_(nullptr),
	 pLiveEnv_(nullptr) {
		handleId_ = 1;
	}

	~ZRtspClientManager();
	int Init();
	int DeInit();

	int OpenStream(const char* url, ZRtspCallBack cb, int connType, const char* user, const char* pwd, int cacheBuffSize);
	int CloseStream(int handle);
	
	//only for ZRtspClient
	void RemoveStream(int handle);

private:
	void LiveEventLoop();
		
	bool threadRun_;
	char eventLoopWatchVariable_;
	std::unique_ptr<BasicTaskScheduler> pLiveScheduler_;
	BasicUsageEnvironment* pLiveEnv_;
	
	std::atomic_int handleId_;
	_zhy_muduo::MutexLock mutexClientMap_;
	std::map<int, std::unique_ptr<ZRtspClient>> clientMap_;

	DISALLOW_COPY_AND_ASSIGN_11(ZRtspClientManager);
};

#define g_ZRtspClientManager (*ZRtspClientManager::instance())
#endif // !ZRTSP_CLIENT_MANAGER_H_
